//
//  SNAllNotifyViewController.m
//  app
//
//  Created by 王少宁 on 15/9/8.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNAllNotifyViewController.h"
#import "SNAllNotifyDataManager.h"
#import "SNAllNotifyCell.h"
#import "HZGoodsSalesViewController.h"

@interface SNAllNotifyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView * myTableView;
@property (nonatomic, retain) SNAllNotifyDataManager * manager;
@property (nonatomic, retain) SNAllNotifyResult * result;
@property (nonatomic, assign) int page;
@end

@implementation SNAllNotifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"物业公告";
    
    self.manager = [SNAllNotifyDataManager shareManager];
    self.result = [[SNAllNotifyResult alloc] initWithTarget:self action:@selector(loadDataWithResult:)];
    
    [self createTableView];
    [self.myTableView.nothingRefreshView show];
    [self refreshData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)createTableView
{
    self.myTableView = CREATE_WITH_FRAME_OF(UITableView, CGPointMake(0, 0),
                                            [self.view.ubo fw:1.0 fh:1.0],
                                            style:UITableViewStyleGrouped);
    self.myTableView.backgroundColor = [XCommon hexStringToColor:kColor_lightGray_cell];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.sectionHeaderHeight = 0;
    self.myTableView.sectionFooterHeight = 0;
    self.myTableView.tableHeaderView = self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    self.myTableView.headerRefreshController = [[UIRefreshHeaderController alloc] initWithTableView:self.myTableView caller:[UBTargetCaller of:self action:@selector(refreshData)]];
    self.myTableView.footerRefreshController = [[UIRefreshFooterController alloc] initWithTableView:self.myTableView caller:[UBTargetCaller of:self action:@selector(loadMoreData)]];
    self.myTableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc] initWithView:self.myTableView viewController:self tryAgain:@selector(tryAgain)];
    [self.view addSubview:self.myTableView];
}

- (void)refreshData
{
    _page = 0;
    [self.manager loadDataWithResult:self.result page:_page];
}

- (void)loadMoreData
{
    _page++;
    [self.manager loadDataWithResult:self.result page:_page];
}

- (void)tryAgain
{
    [self.myTableView.nothingRefreshView show];
    [self.manager loadDataWithResult:self.result page:_page];
}

- (void)loadDataWithResult:(SNAllNotifyResult *)result
{
    [self.myTableView endRefreshingAll];
    if (result.success) {
        [self.myTableView.nothingRefreshView remove];
        [self.myTableView reloadData];
    } else if (result.failure) {
        [self.myTableView.nothingRefreshView activeStop];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.result.noMoreData) {
        return self.result.dataSourceArray.count + 1;
    } else {
        return self.result.dataSourceArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.result.noMoreData) {
        
        if (indexPath.row == self.result.dataSourceArray.count) {
            NoMoreDataCell * cell = [[NoMoreDataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
            cell.backgroundColor = [UIColor clearColor];
            return cell;
        } else {
            static NSString * reuseIdentifier = @"reuseIdentifier";
            SNAllNotifyCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (cell == nil) {
                cell = [[SNAllNotifyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            }
            [cell cellWithModel:self.result.dataSourceArray[indexPath.row]];
            return cell;
        }
        
    } else {
        static NSString * reuseIdentifier = @"reuseIdentifier";
        SNAllNotifyCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            cell = [[SNAllNotifyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        }
        [cell cellWithModel:self.result.dataSourceArray[indexPath.row]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.result.noMoreData && indexPath.row == self.result.dataSourceArray.count) {
        return;
    }
    
    SNAllNotifyModel * model = self.result.dataSourceArray[indexPath.row];
    HZGoodsSalesViewController *salesVC = [[HZGoodsSalesViewController alloc] init];
    salesVC.webTitle = @"最新公告";
    salesVC.webURL = model.url;
    
    [self.navigationController pushViewController:salesVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.result.noMoreData) {
        
        if (indexPath.row == self.result.dataSourceArray.count) {
            return kNoMoreDataCellHeight;
        } else {
            return kAllNotifyCellH;
        }
        
    } else {
        return kAllNotifyCellH;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
